// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IMUStateTests.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Internal.Robotics.Tools.VirtualDriveFeedback
{
    using Microsoft.Robotics.Hardware.DAC;
    using Microsoft.Robotics.Numerics;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// IMUState tests.
    /// </summary>
    [TestClass]
    public class IMUStateTests
    {
        /// <summary>
        /// Test IMUState updates.
        /// </summary>
        /// <remarks>
        /// Primarily gyro average bias calculation.
        /// </remarks>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void StateTests()
        {
            IMUState imu = new IMUState(
                requiredGyroMeasurementsForCalibration: 3,
                gyroCalibrationThreshold: 6)
                {
                    AccelerationXInG = 0, 
                    AccelerationYInG = 0, 
                    AccelerationZInG = 0, 
                    GyroBias = new Vector3(0, 0, 0), 
                    GyroXInRadiansPerSecond = 0, 
                    GyroYInRadiansPerSecond = 0, 
                    GyroZInRadiansPerSecond = 0, 
                };

            AccelerometerState accel = new AccelerometerState() { AccelerationInG = new Vector3(1, 2, 3) };

            imu.IMUHeartbeatUpdate(
                accel, new GyroscopeState() { RotationalVelocityInRadiansPerSecond = new Vector3(0, 0, 0) });

            // accelerometer data propagated
            Assert.AreEqual(1, imu.AccelerationXInG);
            Assert.AreEqual(2, imu.AccelerationYInG);
            Assert.AreEqual(3, imu.AccelerationZInG);

            // initial gyro zero, still calibrating
            Assert.AreEqual(2, imu.GyroBiasCalibrationReadingsRemaining);
            Assert.AreEqual(0, imu.GyroBias.X);
            Assert.AreEqual(0, imu.GyroBias.Y);
            Assert.AreEqual(0, imu.GyroBias.Z);
            Assert.AreEqual(0, imu.GyroXInRadiansPerSecond);
            Assert.AreEqual(0, imu.GyroYInRadiansPerSecond);
            Assert.AreEqual(0, imu.GyroZInRadiansPerSecond);

            imu.IMUHeartbeatUpdate(
                accel, new GyroscopeState() { RotationalVelocityInRadiansPerSecond = new Vector3(1, 2, 3) });
            Assert.AreEqual(imu.GyroBiasCalibrationReadingsRemaining, 1);

            // readings before calibration complete should be unaffected
            Assert.AreEqual(1, imu.GyroXInRadiansPerSecond);
            Assert.AreEqual(2, imu.GyroYInRadiansPerSecond);
            Assert.AreEqual(3, imu.GyroZInRadiansPerSecond);

            // readings above threshold should not affect bias or readings count
            imu.IMUHeartbeatUpdate(
                accel, new GyroscopeState() { RotationalVelocityInRadiansPerSecond = new Vector3(7, 8, 9) });
            Assert.AreEqual(imu.GyroBiasCalibrationReadingsRemaining, 1);
            Assert.AreEqual(7, imu.GyroXInRadiansPerSecond);
            Assert.AreEqual(8, imu.GyroYInRadiansPerSecond);
            Assert.AreEqual(9, imu.GyroZInRadiansPerSecond);

            // readings above threshold should not affect bias or readings count
            imu.IMUHeartbeatUpdate(
                accel, new GyroscopeState() { RotationalVelocityInRadiansPerSecond = new Vector3(7, 1, 1) });
            Assert.AreEqual(imu.GyroBiasCalibrationReadingsRemaining, 1);
            Assert.AreEqual(7, imu.GyroXInRadiansPerSecond);
            Assert.AreEqual(1, imu.GyroYInRadiansPerSecond);
            Assert.AreEqual(1, imu.GyroZInRadiansPerSecond);

            // readings above threshold should not affect bias or readings count
            imu.IMUHeartbeatUpdate(
                accel, new GyroscopeState() { RotationalVelocityInRadiansPerSecond = new Vector3(1, 7, 1) });
            Assert.AreEqual(imu.GyroBiasCalibrationReadingsRemaining, 1);
            Assert.AreEqual(1, imu.GyroXInRadiansPerSecond);
            Assert.AreEqual(7, imu.GyroYInRadiansPerSecond);
            Assert.AreEqual(1, imu.GyroZInRadiansPerSecond);

            // readings above threshold should not affect bias or readings count
            imu.IMUHeartbeatUpdate(
                accel, new GyroscopeState() { RotationalVelocityInRadiansPerSecond = new Vector3(1, 1, 7) });
            Assert.AreEqual(1, imu.GyroBiasCalibrationReadingsRemaining);
            Assert.AreEqual(1, imu.GyroXInRadiansPerSecond);
            Assert.AreEqual(1, imu.GyroYInRadiansPerSecond);
            Assert.AreEqual(7, imu.GyroZInRadiansPerSecond);

            imu.IMUHeartbeatUpdate(
                accel, new GyroscopeState() { RotationalVelocityInRadiansPerSecond = new Vector3(2, 4, 6) });
            Assert.AreEqual(0, imu.GyroBiasCalibrationReadingsRemaining);
                        
            // bias
            Assert.AreEqual(1, imu.GyroBias.X);
            Assert.AreEqual(2, imu.GyroBias.Y);
            Assert.AreEqual(3, imu.GyroBias.Z);

            // gyro 1, 2, 3 (bias-adjusted to zeros)
            imu.IMUHeartbeatUpdate(
                accel, new GyroscopeState() { RotationalVelocityInRadiansPerSecond = new Vector3(1, 2, 3) });
            Assert.AreEqual(0, imu.GyroBiasCalibrationReadingsRemaining);

            Assert.AreEqual(0, imu.GyroXInRadiansPerSecond);
            Assert.AreEqual(0, imu.GyroYInRadiansPerSecond);
            Assert.AreEqual(0, imu.GyroZInRadiansPerSecond);
        }
    }
}
